package com.example.xcsubsystem.util;

import static java.util.Base64.getEncoder;

/**
 * 2024/10/1
 * 在调用统一平台提供的接口时，http header中必须包含这里指定的内容项
 * @author fengyu
 */
public class RemoteHeader {
    public static final String APPKEY = "APPKEY";
    public static final String APPSECRET = "APPSECRET";
    public static final String IV = "IV";

    private final String appKey; // appkey不用加密传输
    private final String appSecret; // 秘钥是加密传输的
    private final String iv; // 每次都会生成新的向量，并进行Base64编码成字符串后传输给统一平台

    /**
     * 用来生成api接口的http header内容
     * @param appKey 统一平台给系统生成的appKey
     * @param appSecret 统一平台给系统生成的appSecret
     * @throws Exception
     */
    RemoteHeader(String appKey, String appSecret) throws Exception {
        byte[] ivBytes = SM4CBCUtils.generateIV();
        this.iv = getEncoder().encodeToString(ivBytes);
        this.appKey = appKey;
        // 对秘钥进行加密传输
        this.appSecret = SM4CBCUtils.encryptBySM4(appSecret, ivBytes, appSecret);
    }

    public static RemoteHeader getInstance (String appKey, String appSecret) throws Exception {
        return new RemoteHeader(appKey, appSecret);
    }

    public String getAppKey() {
        return appKey;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public String getIv() {
        return iv;
    }
}
